using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Spike.ContractBuilder;
using System.ServiceModel;
using System.Diagnostics;

namespace Spike.TestHarness.Host
{
    public partial class EndpointControl : UserControl
    {
        public EndpointControl()
        {
            InitializeComponent();
        }

        DynamicServiceHost _host = null;

        private ServiceHostEndpoint _endpoint;

        public ServiceHostEndpoint Endpoint
        {
            get { return _endpoint; }
            set 
            {
                _endpoint = value; 
            }
        }

        private string _contractName;

        public string ContractName
        {
            get { return _contractName; }
            set { _contractName = value; }
        }


        private void SetInitialState()
        {
            btnStartEndpoint.Text = "Start";
            btnStartEndpoint.BackColor = Color.Green;
        }


        internal void SetEndpoint(string contractName, ServiceHostEndpoint endpoint)
        {
            SetInitialState();
            _endpoint = endpoint;
            _contractName = contractName;

            cbAssemblies.DataSource = _endpoint.InterfaceAssemblies;
            txtContractName.Text = _endpoint.EndpointElement.Contract;
            
            string address = ContractGenerator.Instance.Hosts[contractName].BaseAddress;
            if(_endpoint.EndpointElement.Address.OriginalString!=string.Empty)
                address = address==string.Empty ? _endpoint.EndpointElement.Address.OriginalString
                            : address + "/" + _endpoint.EndpointElement.Address.OriginalString;

            txtAddress.Text = address;
            cbServiceAssembly.DataSource = _endpoint.ServiceAssemblies;
            cbServiceAssembly.DisplayMember = "AssemblyName";
            
        }

        private void cbServiceAssembly_SelectedIndexChanged(object sender, EventArgs e)
        {
            ServiceStub stub = (ServiceStub)cbServiceAssembly.SelectedItem;
            txtServiceClass.Text = stub.ServiceType.FullName;
        }

        private void btnStartEndpoint_Click(object sender, EventArgs e)
        {
            ServiceStub stub = cbServiceAssembly.SelectedItem as ServiceStub;

            if (btnStartEndpoint.Text == "Stop")
            {
                btnStartEndpoint.Text = "Start";
                btnStartEndpoint.BackColor = Color.Green;
                
                StopHost();
            }
            else
            {
                if (StartHost(_contractName, _endpoint.Name, stub))
                {
                    btnStartEndpoint.Text = "Stop";
                    btnStartEndpoint.BackColor = Color.Red;
                }
            }
        }

        public bool StartHost(string contractName, string endpointName, ServiceStub stub)
        {
            bool started = false;
            ServiceHostContract contract = ContractGenerator.Instance.Hosts[contractName];
            ServiceHostEndpoint endpoint = contract.Endpoints[endpointName];

            try
            {
                // stop the host if it is running
                StopHost();

                //Instantiate new ServiceHost 
                _host = new DynamicServiceHost(contract.Name, stub.ServiceType, contract.FileName);

                _host.Open();
                started = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failure opening host: " + ex.Message, "Failure starting host", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //*/
            return started;
        }

        private void StopHost()
        {
            if (_host != null)
            {
                try
                {
                    //Call StopService from your shutdown logic (i.e. dispose method)
                    if (_host.State != CommunicationState.Closed)
                        _host.Close();
                }
                catch { }
            }
        }
    }
}
